<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      :title="popupTitle"
      :async="true"
      width="550px"
      @confirm="handleSubmit"
      @close="handleClose"
    >
      <el-form
        ref="formRef"
        :model="formData"
        label-width="84px"
        :rules="formRules"
      >
        <el-form-item label="套餐编号" prop="sn">
          <el-input
            disabled
            placeholder="请重新打开生成编号"
            v-model="formData.sn"
            clearable
            :maxlength="100"
          />
        </el-form-item>
        <el-form-item label="套餐名称" prop="mealName">
          <el-input
            v-model="formData.mealName"
            placeholder="请输入套餐名称"
            clearable
            :maxlength="100"
          />
        </el-form-item>
        <el-form-item label="图片" prop="mealPic">
          <material-picker v-model="formData.mealPic" />
          <div class="form-tips">请规范图片尺寸</div>
        </el-form-item>
        <el-form-item label="配菜描述" prop="mealDescribe">
          <el-input
            v-model="formData.mealDescribe"
            placeholder="请输入配菜描述"
            clearable
            :maxlength="30"
          />
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from "element-plus";
import Popup from "@/components/popup/index.vue";
import { useDictOptions } from "@/hooks/useDictOptions";
import Upload from "@/components/upload/index.vue";
import feedback from "@/utils/feedback";
import {
  addOrderMeal,
  editOrderMeal,
  findOrderMealDetail,
} from "@/api/admin/index";

const emit = defineEmits(["success", "close"]);
const formRef = shallowRef<FormInstance>();
const popupRef = shallowRef<InstanceType<typeof Popup>>();
const mode = ref("add");
const popupTitle = computed(() => {
  return mode.value == "edit" ? "编辑" : "新建";
});
const formData = reactive({
  id: "",
  sn: "" as string | number,
  mealName: "",
  mealPic: "",
  mealDescribe: "",
  latitude: "",
  deliveryTime: "",
});
const formRules = {
  mealName: [
    {
      required: true,
      message: "请输入套餐名称",
      trigger: ["blur"],
    },
  ],
  mealPic: [
    {
      required: true,
      message: "请上传图片",
      trigger: ["change"],
    },
  ],
  mealDescribe: [
    {
      required: true,
      message: "请输入配菜描述",
      trigger: ["blur"],
    },
  ],
};

const handleSubmit = async () => {
  await formRef.value?.validate();
  mode.value == "edit"
    ? await editOrderMeal(formData)
    : await addOrderMeal(formData);
  popupRef.value?.close();
  feedback.msgSuccess("操作成功");
  emit("success");
};

const open = (type = "add") => {
  mode.value = type;
  popupRef.value?.open();
};

const setFormData = (data: Record<any, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key];
    }
  }
};

const getDetail = async (row: Record<string, any>) => {
  const data = await findOrderMealDetail({
    id: row.id,
  });
  setFormData(data);
};

const handleClose = () => {
  emit("close");
};

defineExpose({
  open,
  setFormData,
  getDetail,
});
</script>
